﻿// FROM : http://groups.google.com/group/microsoft.public.windows.inetexplorer.ie5.programming.components.webbrowser_ctl/browse_thread/thread/8a7f5038432f328e?pli=1

// This code is...very...com'ig...don't change it...or...only..if...you...know what you are doing....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using IDataObject = System.Windows.Forms.IDataObject;
using WebBrowser = System.Windows.Forms.WebBrowser;

namespace ScrumTable.UI.View.WinTouch.Win32Interopt
{

    [ComImport, Guid("00000112-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    public interface IOleObject 
    { 
        void SetClientSite(IOleClientSite pClientSite); 
        void GetClientSite(IOleClientSite ppClientSite); 
        void SetHostNames(object szContainerApp, object szContainerObj); 
        void Close(uint dwSaveOption); 
        void SetMoniker(uint dwWhichMoniker, object pmk); 
        void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk); 
        void InitFromData(IDataObject pDataObject, bool fCreation, uint dwReserved); 
        void GetClipboardData(uint dwReserved, IDataObject ppDataObject); 
        void DoVerb(uint iVerb, uint lpmsg, object pActiveSite, uint lindex, uint hwndParent, uint lprcPosRect); 
        void EnumVerbs(object ppEnumOleVerb); 
        void Update(); 
        void IsUpToDate(); 
        void GetUserClassID(uint pClsid); 
        void GetUserType(uint dwFormOfType, uint pszUserType); 
        void SetExtent(uint dwDrawAspect, uint psizel); 
        void GetExtent(uint dwDrawAspect, uint psizel); 
        void Advise(object pAdvSink, uint pdwConnection); 
        void Unadvise(uint dwConnection); 
        void EnumAdvise(object ppenumAdvise); 
        void GetMiscStatus(uint dwAspect, uint pdwStatus); 
        void SetColorScheme(object pLogpal); 
    } 


    [ComImport, Guid("00000118-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    public interface IOleClientSite 
    { 
        void SaveObject(); 
        void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk); 
        void GetContainer(object ppContainer); 
        void ShowObject(); 
        void OnShowWindow(bool fShow); 
        void RequestNewObjectLayout(); 
    } 


    [ComImport, GuidAttribute("6d5140c1-7436-11ce-8034-00aa006009fa"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComVisible(false)] 
    public interface IServiceProvider 
    { 
        [return: MarshalAs(UnmanagedType.I4)] 
        [PreserveSig] 
        int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject); 
    } 


    [ComImport, GuidAttribute("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown), ComVisible(false)] 
    public interface IAuthenticate 
    { 
        [return: MarshalAs(UnmanagedType.I4)] 
        [PreserveSig] 
        int Authenticate(ref IntPtr phwnd, 
            ref IntPtr pszUsername, 
            ref IntPtr pszPassword 
            ); 
    }





    public class BrowserRemotingProxy : IOleClientSite, IServiceProvider, IAuthenticate 
    { 
        public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b"); 
        public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011); 
        public const int S_OK = unchecked((int)0x00000000);
        private WebBrowser browser;





        public BrowserRemotingProxy(System.Windows.Forms.WebBrowser webBrowser1)
        {
            browser = webBrowser1;
            string oURL = "about:blank";
            webBrowser1.Navigate(oURL);


            object obj = webBrowser1.ActiveXInstance;
            IOleObject oc = obj as IOleObject;
            oc.SetClientSite(this as IOleClientSite);
        }



        #region IOleClientSite Members 


        public void SaveObject() 
        { 
            // NOTE:  Add Form1.SaveObject implementation 
        } 


        public void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk) 
        {
            // NOTE:  Add Form1.GetMoniker implementation 
        } 


        public void GetContainer(object ppContainer) 
        { 
            ppContainer = this; 
        } 


        public void ShowObject() 
        {
            // NOTE:  Add Form1.ShowObject implementation 
        } 


        public void OnShowWindow(bool fShow) 
        {
            // NOTE:  Add Form1.OnShowWindow implementation 
        } 


        public void RequestNewObjectLayout() 
        {
            // NOTE:  Add Form1.RequestNewObjectLayout implementation 
        } 


        #endregion 


        #region IServiceProvider Members 


        public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject) 
        { 
            int nRet = guidService.CompareTo(IID_IAuthenticate);        // Zero returned if the compared objects are equal 
            if (nRet == 0) 
            { 
                nRet = riid.CompareTo(IID_IAuthenticate);                       // Zero returned if the compared objects are equal 
                if (nRet == 0) 
                { 
                    ppvObject = Marshal.GetComInterfaceForObject(this, typeof(IAuthenticate)); 
                    return S_OK; 
                } 
            } 
            ppvObject = new IntPtr(); 
            return INET_E_DEFAULT_ACTION; 
        } 


        #endregion 


        #region IAuthenticate Members 


        public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername, ref IntPtr pszPassword) 
        {
            IntPtr sUser = Marshal.StringToCoTaskMemAuto((((AppScreen)(System.Windows.Application.Current).MainWindow).ScrumData.DataContext.ConnectionInformation.NetworkCredentials.GetCredential(browser.Url, "NTLM").UserName)); 
            IntPtr sPassword = Marshal.StringToCoTaskMemAuto(((((AppScreen)(System.Windows.Application.Current).MainWindow).ScrumData.DataContext.ConnectionInformation.NetworkCredentials.GetCredential(browser.Url, "NTLM").Password))); 
            pszUsername = sUser; 
            pszPassword = sPassword; 
            return S_OK; 
        } 


        #endregion 
    } 

}
